// pages/commoditydetails/morecommoditydetails/morecommoditydetails.js
var utils = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    currentPage: 1,
    type: ""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id
    })
    that.commentslist(that.data.currentPage, that.data.type)
  },
  //商品评价
  commentslist: function(currentPage, type) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/goods/comments_list", {
      be_id: that.data.id,
      page: currentPage,
      type: type
    }, function(res) {
      utils.hideLoading();
      var allpost = that.data.comments;
      if (allpost == undefined) {
        allpost = [];
      }
      for (var i in res.datas.list) {
        allpost.push(res.datas.list[i])
      }
      if (res.datas.list.length < 10) {
        that.setData({
          tishi_title: '已加载全部',
        });
      } else {
        that.setData({
          tishi_title: '加载更多',
        });
      }
      that.setData({
        comments: allpost,
        bad_total: res.datas.bad_total,
        com_total: res.datas.com_total,
        goods_total: res.datas.goods_total,
        ready: res.datas.length == 0 ? 1 : 2,
      })
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (that.data.ready == 2) {
      var page = that.data.currentPage + 1
      that.setData({
        currentPage: page,
      })
      that.commentslist(page, that.data.type)
    }

  },
  //浏览图片
  imgTap: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: e.currentTarget.dataset.http, // 当前显示图片的http链接
      urls: that.data.comments[index].image // 需要预览的图片http链接列表
    })
  },
  swichNav: function(e) {
    var that = this;
    var current = e.currentTarget.dataset.current;
    that.setData({
      currentTab: current,
      type: current,
      currentPage: 1,
      comments: []
    })
    that.commentslist(that.data.currentPage, that.data.type)
  },
  // 点击cover播放，其它视频结束 
  videoPlay: function(e) {
    var audioCtx = wx.createVideoContext('myAudio');
    audioCtx.pause();
    var _index = e.currentTarget.id
    console.log(_index)
    this.setData({
      _index: _index
    })
    //停止正在播放的视频 
    var videoContextPrev = wx.createVideoContext(this.data._index)
    videoContextPrev.stop();
    setTimeout(function() { //将点击视频进行播放   
      var videoContext = wx.createVideoContext(_index)
      videoContext.play();
    }, 500)
  }
})